
//////////////
struct AppData {
    float3 Position	: POSITION;
	float3 Normal	: NORMAL;
    float2 UV		: TEXCOORD0;
    
};

struct VSOUT {
    float4 HPosition: SV_POSITION;
    float2 UV		: TEXCOORD0;
};

int g_sampleType = 0; 
bool g_bFlipY = false;
bool g_bFlipX = false;
bool g_bDrawTexture = true;

float g_rtRatio = 1.0;
uint g_StretchType = 1; // Stretch = 0,	Uniform = 1

float4x4 WorldMat : WORLD_MATRIX;
float4x4 ViewMat : VIEW_MATRIX;
float4x4 ProjMat : PROJ_MATRIX;
float4x4 WorldViewProj : WORLDVIEWPROJ;

float4 g_color : BLEND_COLOR = float4(1,1,1,1);
Texture2D g_Texture : DIFFUSE_COLOR_TEXTURE <
    string ResourceName = "default_color.dds";
    string UIName =  "Diffuse Texture";
    string ResourceType = "2D";
>;

SamplerState g_LinearSampler 
{
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = Border;
    AddressV = Border;
	BorderColor = float4(0, 0, 0, 0);
};


SamplerState g_PointSampler = sampler_state {
    Filter = MIN_MAG_MIP_POINT;
    AddressU = Border;
    AddressV = Border;
	BorderColor = float4(0, 0, 0, 0);
};  

VSOUT mainVS(AppData appIn ) {
	VSOUT ret;
	ret.HPosition = mul(float4(appIn.Position.xyz, 1.0), WorldViewProj);
	ret.UV = appIn.UV;
	return ret;
}

float4 mainPS(VSOUT vin) : SV_Target {
	
	unsigned int texW =0, texH = 0;
	g_Texture.GetDimensions(texW, texH);
    float texRatio = (float)texW / (float)texH;
	
	float2 uv = vin.UV;
	
	
	if (g_bFlipX)
	{
		uv.x = 1 - uv.x;
	}
	if (g_bFlipY)
	{
		uv.y = 1 - uv.y;
	}
	
	if (g_StretchType == 0)
	{
		
	}
	
	else if (g_StretchType == 1)
	{
		if ( g_rtRatio > texRatio )
		{
		    uv.x = (uv.x - 0.5)* g_rtRatio / texRatio + 0.5 ; 
		}
		else
		{
			uv.y = (uv.y - 0.5)* texRatio / g_rtRatio  + 0.5 ; 
		}
	}
	else if (g_StretchType == 2)
	{
		if ( g_rtRatio > texRatio )
		{
		     uv.y = (uv.y - 0.5)*  texRatio / g_rtRatio  + 0.5 ; 
		}
		else
		{
			 uv.x = (uv.x - 0.5)* g_rtRatio / texRatio + 0.5 ;
			
		}
	}
	
	float4 retColor = float4(0,0,0,0);
	if (g_bDrawTexture)
	{	
		if (g_sampleType == 0)
		{
			retColor = g_Texture.Sample(g_PointSampler, uv);
		}
		else
		{
			retColor = g_Texture.Sample(g_LinearSampler, uv);
		}
		retColor = retColor*g_color; 
	}
	else
	{
		retColor = g_color;
	}
	return retColor;
}


///// TECHNIQUES /////////////////////////////
RasterizerState DisableCulling
{
    CullMode = NONE;
};

DepthStencilState DepthEnabling
{
	DepthEnable = FALSE;
};

BlendState blendState
{
	BlendEnable[0] = TRUE;
	SrcBlend = SRC_ALPHA ;
	DestBlend = INV_SRC_ALPHA;
};

technique10 Main10 <
	string Script = "Pass=p0;";
> {
    pass p0 <
	string Script = "Draw=geometry;";
    > {
        SetVertexShader( CompileShader( vs_4_0, mainVS() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_4_0, mainPS() ) );
                
        SetRasterizerState(DisableCulling);       
		SetDepthStencilState(DepthEnabling, 0);
		SetBlendState(blendState, float4( 1.0f, 1.0f, 1.0f, 1.0f ), 0xFFFFFFFF);
    }
}

